<template>
  <div class="layout">
    <el-row>
      <el-col :span="12" :offset="0" :xs="0"></el-col>
      <el-col :span="12" :offset="0" :xs="24">
        <el-form
          class="login"
          :model="longUser"
          :rules="loginRule"
          ref="loginIns"
        >
          <h1>Hello</h1>
          <h2>欢迎来到系统</h2>
          <el-form-item prop="username">
            <el-input :prefix-icon="User" v-model="longUser.username">
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              :prefix-icon="Lock"
              v-model="longUser.password"
              show-password
            >
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              size="default"
              @click="onLogin"
              :loading="isLoading"
              >登录</el-button
            >
            <el-button
              type="primary"
              size="default"
              @click="resetForm(loginIns)"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from "@element-plus/icons-vue";
import { reactive, ref } from "vue";
import type { LoginForm } from "@/api/index.ts";
import useUserStore from "@/store/user.ts";
import { useRouter } from "vue-router";
import { ElNotification, FormInstance, FormRules } from "element-plus";
const longUser: LoginForm = reactive({
  username: "admin",
  password: "111111",
});
// ref 登录按钮
const isLoading = ref(false);
// 表单填写校验
const loginIns = ref<FormInstance>(); // 表单对象
// 使用store实例
const userStore = useUserStore();
// 编程路由器
const $router = useRouter();
const onLogin = async () => {
  // 第一步：校验表单
  let formCheck: boolean = await loginIns.value?.validate();
  if (!formCheck) return;

  try {
    // 开启按钮加载
    isLoading.value = true;
    // 执行登录逻辑
    await userStore.userLogin(longUser);
    // 结束按钮加载
    isLoading.value = false;
    //登录成功跳转
    $router.push({ path: "/" });
    // 登录成功提示框通知
    ElNotification({
      title: "登录成功",
      message: "下午好，欢迎回来！",
      type: "success",
    });
  } catch (error) {
    ElNotification({
      title: "登录失败",
      message: `${error}`,
      type: "error",
    });
    isLoading.value = false;
  }
};

// 自定义校验规则
const checkUserName = (_rule: any, value: any, callback: any) => {
  if (!/^[A-Za-z0-9\u4e00-\u9fa5]+$/g.test(value)) {
    console.log(123);

    callback(new Error("不能为特殊字符"));
    // callback('不能为特殊字符')
  } else {
    callback();
  }
};

const loginRule = reactive<FormRules<LoginForm>>({
  username: [
    { required: true, message: "请输入账号", trigger: "blur" },
    { min: 2, max: 7, message: "长度不对", trigger: "blur" },
    { validator: checkUserName, trigger: "blur" },
  ],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
});

// 表单填写重置
const resetForm = (formEl: FormInstance | undefined) => {
  console.log("===》" + formEl);
  formEl!.resetFields();
  if (!formEl) return;
};
document.title = "登录";
</script>

<style scoped lang="scss">
.layout {
  width: 100%;
  height: 100vh;
  background: url("@/assets/images/background.jpg") no-repeat;
  background-size: cover;

  .login {
    width: 80%;
    text-align: center;
    position: relative;
    top: 30vh;
    background: url("@/assets/images/login_form.png") no-repeat;
    background-size: cover;
    padding: 40px;
  }
}
</style>
